[[ package gen;

import java.util.*;

import se.rupy.http.*;
import se.rupy.memory.*;
import se.rupy.sprout.*;
import se.rupy.content.*;

public class publish extends Sprout {
	public int index() { return 3; }
	public String path() { return "/publish"; }
	public void filter(Event event) throws Event, Exception {
		Output out = event.output();
		Object key = event.session().get("key");
		Article article = Article.get(key);
		User user = User.get(key);
		
		Calendar date = Calendar.getInstance();
		
		int day = event.medium("day");
		int month = event.medium("month");
		int year = event.medium("year");

		if(!(day > 0 && month > 0 && year > 0)) {
			event.query().put("day", date.get(Calendar.DAY_OF_MONTH));
			event.query().put("month", date.get(Calendar.MONTH) + 1);
			event.query().put("year", date.get(Calendar.YEAR));
		}
		
		if(event.string("title").length() == 0 && 
		   event.string("body").length() == 0) {
			//Article.remove(key);
			//article = null;
		} ]]

    <table border="0">
      <form name="article" action="publish" method="post">
        <tr>
          <td colspan="3">
            <a href="/logout" class="button">
              <span>[[ i18n("Logout") ]]</span>
            </a>

            <a href="/user" class="button">
              <span>[[ i18n("Profile") ]]</span>
            </a>

[[		if(user != null && user.permit("ADMIN")) { ]]

            <a href="/label" class="button">
              <span>[[ i18n("Labels") ]]</span>
            </a>

[[		} ]]
          
          </td>
        </tr>

[[		if(event.string("error").length() > 0) { ]]

        <tr>
          <td colspan="3">
            <font color="red"><i>[[ event.string("error") ]]</i></font>
          </td>
        </tr>

[[		} ]]

        <tr>
          <td align="right"><font color="[[ i18n("#F88017") ]]">[[ i18n("Title") ]]</font>&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td colspan="2">
            <input type="text" class="large" name="title" value="[[ event.string("title") ]]" tabindex="1"/>
          </td>
        </tr>
        <tr>
          <td align="right"><font color="[[ i18n("#F88017") ]]">[[ i18n("Date") ]]</font>&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td colspan="2">
            <!-- // ISO 8601 -->
            <select name="year" class="year" tabindex="2">
            
[[		for(int i = 2005; i < date.get(Calendar.YEAR) + 1; i++) { ]]

              <option value="[[ i ]]"[[ event.query().medium("year", 1990) == i ? " selected" : "" ]]>[[ i ]]</option>

[[		} ]]

            </select>
            <select name="month" class="month" tabindex="3">
            
[[		for(int i = 1; i < 13; i++) { ]]

              <option value="[[ i ]]"[[ event.medium("month") == i ? " selected" : "" ]]>[[ i18n(User.month[i - 1]) ]]</option>

[[		} ]]

            </select>
            <select name="day" class="day" tabindex="4">
            
[[		for(int i = 1; i < 32; i++) { ]]

              <option value="[[ i ]]"[[ event.medium("day") == i ? " selected" : "" ]]>[[ i ]]</option>

[[		} ]]

            </select>
          </td>
        </tr>
        <tr>
          <td colspan="3" align="right">
            <textarea class="large" name="body" rows="5" tabindex="6">[[ event.string("body") ]]</textarea>
          </td>
        </tr>
        <tr>
          <td align="right"><font color="[[ i18n("#3B9C9C") ]]">[[ i18n("Label") ]]</font>&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <select name="label" class="label" tabindex="6">
              <option value="--">None</option>
            </select>
          </td>
          <td>
            <a href="" class="button" onclick="addLabel(); return false;">
              <span>[[ i18n("Add") ]]</span>
            </a>
          </td>
        </tr>

[[		if(article != null) { ]]

        <tr>
          <td colspan="3">
          
[[			Iterator it = article.child(FILE).iterator();
			while(it.hasNext()) {
				Node file = (Node) it.next();
				Data name = file.meta(FILE_NAME);
				Data type = file.meta(FILE_TYPE);
				
				if(type != null) {
					if(type.equals("IMAGE")) { ]]

            <a href="[[ File.path(event, file, name, "-400.jpeg") ]]">
              <img src="res/file/image.gif" alt="[[ File.path(event, file, name, "-400.jpeg") ]]"/>
            </a>

[[ 					}
					else if(type.equals("VIDEO")) { ]]

            <a href="[[ File.path(event, file, name, ".flv") ]]">
              <img src="res/file/video.gif" alt="[[ File.path(event, file, name, ".flv") ]]"/>
            </a>

[[ 					}
					else if(type.equals("AUDIO")) { ]]

            <a href="[[ File.path(event, file, name, ".mp3") ]]">
              <img src="res/file/audio.gif" alt="[[ File.path(event, file, name, ".mp3") ]]"/>
            </a>

[[					}
				}
				else { ]]

            <a href="[[ File.path(event, file, name, "") ]]">
              <img src="res/file/binary.gif" alt="[[ File.path(event, file, name, "") ]]"/>
            </a>

[[				}
 			} ]]

          </td>
        </tr>

[[		} ]]
        
        <tr>
          <td colspan="3">
            <a href="" class="arrow" onclick="document.article.submit(); return false;">
              <span>[[ i18n("Publish") ]]</span>
            </a>
          </td>
        </tr>
      </form>
      
[[		if(article != null && article.getId() > 0) { ]]
      
      <form enctype="multipart/form-data" name="upload" action="upload" method="post">
        <tr>
          <td colspan="3">
            <a class="button overflow">
              <input name="file" type="file" class="file" style="right: 25px;" onchange="document.upload.submit()"/>
              <span>[[ i18n("Upload") ]]</span>
            </a>
          </td>
        </tr>
      </form>
      
[[		} ]]
      
    </table>

[[ } 
} ]]